﻿
/*
Plugin=选择计算
Name1=选择计算
Command1=SelectCalc
Author=runany
mod=BoBO
Version=1.0
*/

SelectCalc(selectZz)
{
selectZz := QZdata("Text")
selectCheck:=Trim(selectZz," `t`n`r")
     openFlag:=false
     calcFlag:=false
     calcResult:=""
     selectResult:=""
     Loop, parse, selectZz, `n, `r
     {
               S_LoopField=%A_LoopField%
               if(S_LoopField=""){
					if(calcResult)
						calcResult:=A_LoopField "`n"
					if(selectResult)
						selectResult:=A_LoopField "`n"
					continue
				}
				;；一键计算数字加减乘除    
				if(RegExMatch(S_LoopField,"^[\(\)\.\d]+[+*/-]+[\(\)\.+*/-\d]+($|=$)")){
						formula:=S_LoopField
						if(RegExMatch(S_LoopField,"^[\(\)\.\d]+[+*/-]+[\(\)\.+*/-\d]+=$")){
							StringTrimRight, formula, formula, 1
						}
						calc:=Eval(formula)
						selectResult.=A_LoopField
						if(RegExMatch(S_LoopField,"^[\(\)\.\d]+[+*/-]+[\(\)\.+*/-\d]+=$")){
							calcFlag:=true
							selectResult.=calc
						}else{
							calcResult.=calc "`n"
						}
						selectResult.="`n"
						openFlag:=true
						continue
					}
     }
          if(calcResult){
				StringTrimRight, calcResult, calcResult, 1
				ToolTip,%calcResult%
				Clipboard:=calcResult
				SetTimer,RemoveToolTip,% (calcResult="?") ? 1000 : 3000
				
          }
          if(calcFlag && selectResult){
				StringTrimRight, selectResult, selectResult, 1
				Send_Str_Zz(selectResult)
          }
          ;if(openFlag)
		  ;MsgBox
          return 
return 
}
return

escapeString(string){
	string:=RegExReplace(string, "('|""|&|\\|\\n|\\r|\\t|\\b|\\f)", "\$1")
	string:=RegExReplace(string, "\R", "\n")
}
Send_Str_Zz(strZz,tf=false){
	if(tf){
		strZz:=Get_Transform_Val(strZz)
	}
	Candy_Saved:=ClipboardAll
	Clipboard:=strZz
	SendInput,^v
	Sleep,200
	Clipboard:=Candy_Saved
}
;~;获取变量展开转换后的值
Get_Transform_Val(var){
	try{
		Transform,varTemp,Deref,%var%
		return varTemp
	}catch{
		return var
	}
}

Eval(Expr, Format := FALSE)
{
    static obj := ""    ; par cuestiones de rendimiento
    if ( !obj )
        obj := ComObjCreate("HTMLfile")

    Expr := StrReplace( RegExReplace(Expr, "\s") , ",", ".")
  , Expr := RegExReplace(StrReplace(Expr, "**", "^"), "(\w+(\.*\d+)?)\^(\w+(\.*\d+)?)", "pow($1,$3)")    ; 2**3 -> 2^3 -> pow(2,3)
  , Expr := RegExReplace(Expr, "=+", "==")    ; = -> ==  |  === -> ==  |  ==== -> ==  |  ..
  , Expr := RegExReplace(Expr, "\b(E|LN2|LN10|LOG2E|LOG10E|PI|SQRT1_2|SQRT2)\b", "Math.$1")
  , Expr := RegExReplace(Expr, "\b(abs|acos|asin|atan|atan2|ceil|cos|exp|floor|log|max|min|pow|random|round|sin|sqrt|tan)\b\(", "Math.$1(")

  , obj.write("<body><script>document.body.innerText=eval('" . Expr . "');</script>")
  , Expr := obj.body.innerText

    return InStr(Expr, "d") ? "" : InStr(Expr, "false") ? FALSE    ; d = body | undefined
                                 : InStr(Expr, "true")  ? TRUE
                                 : ( Format && InStr(Expr, "e") ? Format("{:f}",Expr) : Expr )
} ; CREDITS (tidbit) - https://autohotkey.com/boards/viewtopic.php?f=6&t=15389







